home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / educate / wordy442.zip / UNSCR.C < prev    next >
C/C++ Source or Header  |  1996-07-05  |  7KB  |  266 lines

  1. /**************************************************************************/
  2. /*                               UNSCRAMBLE Utility                       */
  3. /*                                                                        */
  4. /*                                     M\Cooper                           */
  5. /*                                    PO Box 237                          */
  6. /*                            St. David, AZ 85630-0237                    */
  7. /*                        -------------------------------                 */
  8. /*                        Email:  thegrendel@theriver.com                 */
  9. /*                                                                        */
  10. /*                                                                        */
  11. /*                   $2.00 to register the entire WORDY package           */
  12. /*                                                                        */
  13. /**************************************************************************/
  14.  
  15.  
  16. /**********************************WORDTEST********************************/
  17. /*       Function tests if word is constructible from Letterset            */
  18. /*                 Args in: char *letterset, char *word                          */
  19. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  20. /**************************************************************************/
  21.  
  22. #include <conio.h>
  23. #include <ctype.h>
  24. #include "srch.h"
  25.  
  26.  
  27. #define FILE_OPENING_ERROR 3
  28. #define FILENAME_MAXLEN 40
  29. #define CR "\n"
  30. #define FILE_SUFFIX ".wds"
  31. #define MAXLEN 30
  32. #define LINE_LEN 80
  33. #define NOARGS 1
  34. #define INCREMENT 1
  35. #define SPACE ' '
  36. #define XOUT '@'
  37. #define WILDCARD '?'
  38.  
  39. #define BUFFERSIZE 16384
  40.  
  41. #define NOPOSITION -1
  42. #define NOLETTER '@'
  43. #define WORDLEN 40
  44. #define XTRALETS 2
  45.  
  46. typedef enum { FALSE, TRUE } Boolean;
  47. typedef struct { char wd [WORDLEN]; int position; char letter; } Wboundary;
  48.  
  49.  
  50. void getword( char *letter_set, size_t w_len, char *filenam, int pos, char c );
  51. void center( char *strng );
  52. Wboundary parse ( char *wordinfo );
  53.  
  54. /****************************globals****************************************/
  55. char ad[] =
  56.      "UNSCRAMBLE utility by M\\Cooper, PO Box 237, St. David, AZ 85630-0237";
  57. char Lset [MAXLEN];
  58. /**************************************************************************/
  59.  
  60. void main( int argc, char **argv )
  61. {
  62.  
  63.    char letterset [MAXLEN],
  64.         filenam [FILENAME_MAXLEN];
  65.    Wboundary Wb;
  66.  
  67.      if( argc == NOARGS )
  68.         {
  69.         clrscr();
  70.         puts( "Enter a LETTERSET to test ... " );
  71.         gets( letterset );
  72.      strcpy( filenam, "word.lst" );
  73.      strcpy( Lset, letterset );
  74.      Wb = parse ( letterset );
  75.      getword( Wb.wd, strlen( Wb.wd ), filenam, Wb.position, Wb. letter );
  76.  
  77.         }
  78.  
  79.   else   
  80.       if( argc == NOARGS + 1 )
  81.          {
  82.          strcpy( filenam, "word.lst" );
  83.          strcpy( letterset, *( argv + 1) );
  84.          strcpy( Lset, letterset );
  85.          Wb = parse ( letterset );
  86.          getword( Wb.wd, strlen( Wb.wd ), filenam, Wb.position, Wb. letter );
  87.          }
  88.      else
  89.         {
  90.         strcpy( letterset, *( argv + 1 ) );
  91.      strcpy( Lset, letterset );
  92.      strcpy( filenam, *( argv + 2 ) );
  93.      Wb = parse ( letterset );
  94.      getword( Wb.wd, strlen( Wb.wd ), filenam, Wb.position, Wb. letter );
  95.         }
  96. }
  97.  
  98.  
  99.  
  100. Boolean wordtest( char *letterset, char *word )
  101. {
  102.     Boolean error_flag = TRUE;
  103.     static char dup_lset[ MAXLEN ];
  104.     register char *letpos;
  105.  
  106.      strcpy( dup_lset, letterset );
  107.          
  108.         while( *word )
  109.             {
  110.             if( ( letpos  = strchr( dup_lset, *word++ ) ) != NULL )
  111.                 *letpos = XOUT;     //As long as letter contained...
  112. /****************Wildcards now allowed***********************/
  113.       else
  114.          if( ( letpos = strchr( dup_lset, WILDCARD ) ) != NULL ) 
  115.             *letpos = XOUT;  //Or wildcard character...
  116.  
  117.             else
  118.                 { error_flag = FALSE; break; } //test fails (not contained)
  119.             }
  120.  
  121.         return( error_flag );
  122. }
  123.  
  124. /*************************************************************/
  125. void getword( char *letter_set, size_t w_len, char *filename, int pos, char c )
  126. {
  127.  
  128.     char    l_set [ MAXLEN ],
  129.         word [ MAXLEN ],
  130.         tempstr [ MAXLEN + 1 ],
  131.         bar [ LINE_LEN + 1 ],
  132.         double_bar [ LINE_LEN + 1 ],
  133.    ts [ MAXLEN ];
  134.  
  135.     FILE *fptr;
  136.     long wcount = 0L;
  137.  
  138.        memset( bar, '-', LINE_LEN );
  139.        *( bar + LINE_LEN ) = NULL;
  140.        memset( double_bar, '=', LINE_LEN );
  141.        *( double_bar + LINE_LEN ) = NULL;
  142.  
  143.        /*************opening credits*************/
  144.        clrscr();
  145.        printf( double_bar );
  146.        strcpy( tempstr, ad );
  147.        center ( tempstr );
  148.        printf( tempstr );
  149.        printf( CR );
  150.        printf( double_bar );
  151.        printf( CR );
  152.        /****************************************/
  153.  
  154.  
  155.        strcpy ( l_set, letter_set );
  156.        strcat ( letter_set, CR );
  157.  
  158.  
  159.        if( !( fptr = fopen( filename, "rt" ) ) )
  160.          {
  161.          printf( "\7\7\7Cannot open wordfile %s!", filename );
  162.          exit( FILE_OPENING_ERROR );
  163.          }
  164.       if( setvbuf( fptr, NULL, _IOFBF, BUFFERSIZE ) )
  165.          exit( FILE_OPENING_ERROR );
  166.  
  167.        /**************'Wait' Message************/
  168.        printf( CR CR );
  169.        printf( "WORKING...\n\n" );
  170.        printf( "This will take a few seconds...\n" );
  171.        printf( "Please be patient.\n\n" );
  172.        printf( "Now searching 100,000+ word file for possible solutions...\n\n" );
  173.        /*****************************************/
  174.  
  175.  
  176.  
  177.  
  178.  
  179.        sprintf( tempstr, "Word(s) unscrambled from: %s\n", strupr( Lset ) );
  180.        center( tempstr );
  181.        printf( double_bar );
  182.        printf( tempstr );
  183.        printf( double_bar );
  184.        printf( CR );
  185.  
  186.  
  187.          /*********************Main Loop*************/     
  188.           while( fgets( word, MAXLEN, fptr ) != NULL )
  189.  
  190.       if( wordtest( letter_set, word ) )
  191.          if( strlen( word ) == w_len + INCREMENT && ( ( *( word + pos - 1 ) == c ) || pos == NOPOSITION ) )
  192.            {
  193.            printf( "%s", word );
  194.            wcount++;
  195.            }
  196.  
  197.  
  198.       if( wcount == INCREMENT )
  199.          strcpy( ts, "word" );
  200.       else
  201.          strcpy( ts, "words" );
  202.  
  203.           /*******************************************/
  204.  
  205.           printf( bar );
  206.           sprintf( tempstr, "%ld %s can be unscrambled from %s.",
  207.                  wcount, ts, Lset );
  208.           center( tempstr );              
  209.           printf( tempstr );
  210.           printf( "\n\n" );
  211.  
  212.           center( ad );
  213.           printf( ad );
  214.       printf( "\7\n\n" );
  215.  
  216.           fcloseall();
  217.  
  218.  
  219. }
  220.  
  221.  
  222.  
  223. void center( char *str )
  224. {
  225.    int padding;
  226.    char st [ LINE_LEN + INCREMENT ];
  227.  
  228.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  229.      memset( st, SPACE, padding );
  230.      *( st + padding ) = NULL;  //Terminate string
  231.      strcat( st, str );
  232.      strcpy( str, st );
  233.  
  234.      return;
  235. }
  236.  
  237.  
  238. Wboundary parse ( char *wordinf )
  239. {
  240.    Wboundary wp;
  241.    char *wptr = wordinf,
  242.          wxtra [ XTRALETS ];
  243.  
  244.        strcpy( wp.wd, wptr );
  245.        wp.position = NOPOSITION;
  246.        wp.letter = NOLETTER;
  247.  
  248.        while ( *wordinf )
  249.          {
  250.          if( isdigit ( *wordinf ) )
  251.             {
  252.             wp.position = atoi ( wordinf );
  253.             wp.letter = *( wordinf + 1 );
  254.             *wordinf = NULL;
  255.             strcpy( wp.wd, wptr );
  256.             *wxtra = wp.letter;
  257.             *( wxtra + 1 ) = NULL;
  258.             strcat( wp.wd, wxtra );
  259.             break;
  260.             }
  261.           wordinf++;
  262.           }
  263.  
  264.       return( wp );
  265. }
  266.